package com.eric.controller;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.util.SaResult;
import com.eric.domain.bso.UserBso;
import com.eric.domain.dto.SysUserDto;
import com.eric.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 新哥
 * @since 2024-03-12
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/sys-user")
public class SysUserController {
    private final ISysUserService userService;

    /*
     *修改一个用户信息
     * 这个接口，要求必须有id，这个在前端获取到user信息时会获取到
     * 这个接口不允许修改loginname值
     * 接口只核对id，对其他字段，只要传进来，就会修改
     * */
    @PutMapping("/modifyUser")
    @SaCheckPermission("sys:modify")
    public SaResult modifyUser(SysUserDto userDto,MultipartFile profilePhoto,HttpServletRequest request) {
     return  userService.modifyUser(userDto,profilePhoto,request);
    }

    /*
     *  新建一个用户，包含大头照
     *   管理员权限添加
     * */
    @PostMapping("/saveOneUser")
    @SaCheckRole(value = {"sys:coder","sys:superAdmin","sys:admin","sys:manager"},mode = SaMode.OR)
    public SaResult saveOneUser(UserBso userBso, MultipartFile profilePhoto, HttpServletRequest request){
        return userService.saveOneUser(userBso,profilePhoto,request);
    }

    /*
     * 分页查询数据中user数据
     *  current:第几页开始
     *  pageSize:每页显示多少条数据
     * */
    @GetMapping("/all")
    @SaCheckRole(value = {"sys:coder","sys:superAdmin","sys:admin"},mode = SaMode.OR)
    public SaResult getAllUserOnPage(Integer current,Integer pageSize){
        return userService.getAllOnPage(current,pageSize);
    }

    /*
     * 通过User的id批量删除用户
     * */
    @DeleteMapping("/deletes")
    @SaCheckPermission("sys:delete")
    @SaCheckRole(value = {"sys:coder","sys:superAdmin","sys:admin"},mode = SaMode.OR)
    public SaResult deleteUsers(@RequestBody List<Integer>ids){
        return userService.deleteByIds(ids);
    }

    //    根据姓名模糊查询用户-可能有多个
    @GetMapping("/getAllByName")
    @SaCheckRole(value = {"sys:coder","sys:superAdmin","sys:admin"},mode = SaMode.OR)
    public SaResult getAllByName(String name,Integer current,Integer pageSize){
        return userService.getAllByNameService(name,current,pageSize);
    }

    //    查询当前登录用户的详细信息
    @GetMapping("/getUserDetail")
    public SaResult getUserDetail(){
        return userService.getUsetDetailService();
    }

    //    根据id删除一个用户
    @DeleteMapping("/deleteOne")
    @SaCheckPermission("sys:delete")
    @SaCheckRole(value = {"sys:coder","sys:superAdmin","sys:admin"},mode = SaMode.OR)
    public SaResult deleteOne(Integer id){
        return userService.deleteOne(id);
    }

    //    根据id获取用户详细信息
    @GetMapping("/{id}")

    public SaResult getUserById(@PathVariable("id")  Integer id){
        return userService.getUserById(id);
    }


}
